var express = require('express');
var router = express.Router();
var fs = require('fs')
var path = require('path')
var Promise = require('bluebird')

var userCtrl = require('../controllers/user/userController')
var applyCtrl = require('../controllers/user/applyManageController')
var suggestCtrl = require('../controllers/user/userSuggestController')
var adminCtrl = require('../controllers/manage/adminController')
//var pageCtrl = require('../controllers/pageController')
var testCtrl = require('../controllers/test/testController')
var activeCtrl = require('../controllers/active/activeController')
var postCtrl = require('../controllers/post/postController')



//  管理员认证接口
// router.use(function(req, res, next) {
//     var route = req.path.toLowerCase()
//     if (route.indexOf('admin') > -1 && route.indexOf('login') == -1 && !req.session.adminId) {
//         res.redirect('/admin/loginPage')
//     } else {
//         next()
//     }
// })

//  验证码认证接口
// router.use(function(req, res, next) {
//     if (req.body.verifyCode) {
//         if (req.session.verifyCode != req.body.verifyCode) {
//             return res.json({
//                 code: 122,
//                 msg: '验证码错误'
//             })
//         }
//         if (req.session.bindEmail != req.body.user.email) {
//             return res.json({
//                 code: 122,
//                 msg: '邮箱已更换，请重新发送验证码'
//             })
//         }
//         if (req.body.isChangeBindEmail && req.body.password != req.body.user.password) {
//             return res.json({
//                 code: 122,
//                 msg: '密码错误'
//             })
//         }
//         next()
//     } else {
//         next()
//     }
// })

//------------------后台页面渲染接口-----------------------//
// router.get('/admin', adminCtrl.adminHomePage)
// router.get('/admin/testPage', adminCtrl.adminTestPage)
// router.get('/admin/postPage', adminCtrl.adminPostPage)
// router.get('/admin/sitePage', adminCtrl.adminSitePage)
// router.get('/admin/userPage', adminCtrl.adminUserPage)
// router.get('/admin/loginPage', adminCtrl.adminLoginPage)


//------------------用户注册、登陆、个人中心接口--------------//

//router.get('/user/list', userCtrl.getAll)

router.get('/user/skip/:skip/limit/:limit', userCtrl.getList)
router.get('/user/login', userCtrl.login)
router.post('/user/register', userCtrl.register)

router.get('/user/baseInfo/id/:id', userCtrl.getUserInfo)
router.put('/user/baseInfo/id/:id', userCtrl.updateUserInfo)
router.delete('/user/baseInfo/id/:id', userCtrl.remove)

router.put('/user/password/id/:id', userCtrl.changePassword)

router.get('/user/verifyCode', userCtrl.getVerifyCode)

//  申请管理员
router.post('/user/applyManage', applyCtrl.add);
//  投诉建议
router.post('/user/suggest', suggestCtrl.add)


//-----------------后台页面接口位置----------------//

//  管理员登陆
router.get('/m/login', adminCtrl.login)
router.get('/m/adminList', adminCtrl.getAdmins)
router.get('/m/apply/skip/:skip/limit/:limit', applyCtrl.getList)
router.put('/m/apply/id/:id', applyCtrl.update)
router.get('/m/suggest/skip/:skip/limit/:limit', suggestCtrl.getList)
router.put('/m/suggest/id/:id', suggestCtrl.update)


//-----------------习题接口位置----------------//

router.get('/test/type/:type/skip/:skip/limit/:limit', testCtrl.getList);
//  创建新习题总接口
router.post('/test/newTest/type/:type', testCtrl.create);
router.put('/test/updateTest/type/:type', testCtrl.update)
    //  获取单个习题或答案内容
router.get('/test/type/:type/id/:id', testCtrl.getOne)
    //  删除单个习题后答案数据
router.delete('/test/type/:type/id/:id', testCtrl.removeTopic)

//-----------------最新动态----------------//
router.get('/active/skip/:skip/limit/:limit', activeCtrl.getActiveList)

//-----------------帖子接口位置----------------//
//  获取帖子列表
router.get('/post/skip/:skip/limit/:limit', postCtrl.getPost)

//  获取单个帖子信息
router.get('/post/id/:id', postCtrl.getOne)
router.delete('/post/id/:id', postCtrl.remove)

//  发表帖子
router.post('/post', postCtrl.add)

//  发表回复
router.post('/post/comment/id/:id', postCtrl.addComment)


// //  收藏帖子
// router.post('/post/collect/id/:id', postCtrl.addCollect)



module.exports = router;